public class Office2Pdf {
    // OpenOffice的安装目录,默认会安装到c盘下
    private static String OpenOffice_HOME = "E:/OpenOffice4/program";
    // 启动服务的命令
    private static String command = "soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\"";
    
    private static Process process = null;
    /**
     * 核心转pdf方法
     * 
     * @param sourcefile 被转文件
     * @param targetfile 转换后的文件
     * @return
     */
    public static void  convertTo(File sourcefile, File targetfile)
    {
         try {
             // 启动方法
             if(process == null){
                 process = startOpenOffice();
             }
             
             //8100就是启动openoffice的端口,
             OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);                      
             connection.connect();
             //创建openoffice文档转换类
             DocumentConverter converter = new OpenOfficeDocumentConverter(connection); 
             //转换,传入源文件和目标文件;
             converter.convert(sourcefile, targetfile);
             //断开于openoffice服务的连接
             connection.disconnect();
         } catch (ConnectException e) { 
             e.printStackTrace();
         }
     }
    
    /**
     * 启动openOffice服务
     */
    public static Process startOpenOffice(){
         // 启动OpenOffice的服务的完整命令
         String fullCommand = OpenOffice_HOME + command;
        
         try {
            return Runtime.getRuntime().exec(fullCommand);
        } catch (IOException e) {
            e.printStackTrace();
        }
         return null;
    }
}